#!bin/bash
#研发环境自动更新脚本
#通过判断git更新目录确定更新打包更新哪个模块
curPath=$(readlink -f "$(dirname "$0")")

FIND_FILE=${curPath}/pull.log
ALL_BUILD="false"
# 后动触发更新 修改README.md 公共组件修改不频繁 所有需要手动触发
if [ `grep -c "README.md" $FIND_FILE` -ne '0' ] ;then
  ALL_BUILD="true"
  # 全局打包
  mvn clean install -Dmaven.test.skip=true -P dev
fi
if [[ "${ALL_BUILD}" == "false" ]] ; then
  if [ `grep -c "common-utils" $FIND_FILE` -ne '0' ]; then
    cd ${curPath}/common-utils
    mvn clean install -Dmaven.test.skip=true -P dev
  fi
  if [ `grep -c "common-model" $FIND_FILE` -ne '0' ]; then
    cd ${curPath}/common-model
    mvn clean install -Dmaven.test.skip=true -P dev
  fi
  if [ `grep -c "common-log" $FIND_FILE` -ne '0' ]; then
    cd ${curPath}/common-log
    mvn clean install -Dmaven.test.skip=true -P dev
  fi
  if [ `grep -c "public-framework" $FIND_FILE` -ne '0' ]; then
    cd ${curPath}/public-framework
    mvn clean install -Dmaven.test.skip=true -P dev
  fi
  if [ `grep -c "public-service" $FIND_FILE` -ne '0' ]; then
    cd ${curPath}/public-service
    mvn clean install -Dmaven.test.skip=true -P dev
  fi
  if [ `grep -c "common-config" $FIND_FILE` -ne '0' ]; then
    cd ${curPath}/common-config
    mvn clean install -Dmaven.test.skip=true -P dev
  fi
  if [ `grep -c "common-link" $FIND_FILE` -ne '0' ]; then
    cd ${curPath}/common-link
    mvn clean install -Dmaven.test.skip=true -P dev
  fi
  if [ `grep -c "common-dubbo-consumer" $FIND_FILE` -ne '0' ]; then
    cd ${curPath}/common-dubbo/common-dubbo-consumer
    mvn clean install -Dmaven.test.skip=true -P dev
  fi
fi

# 1.启动job-admin
#if [ `grep -c "ccf-job" $FIND_FILE` -ne '0' ] ;then
#  #删除容器
#  if [ $(docker ps -aq --filter name=ccf-job-admin) ]; then
#    docker rm -f ccf-job-admin
#  fi
#  #删除镜像
#  if [[ $(docker images |grep ccf/ccf-job-admin) ]]; then
#    docker rmi ccf/ccf-job-admin:v1.0
#  fi
#  cd ${curPath}/ccf-job
#  mvn clean install -Dmaven.test.skip=true -P dev
#  cd ${curPath}/ccf-job/ccf-job-admin
#  docker build    -t ccf/ccf-job-admin:v1.0 .
#  docker run --restart=always   --name ccf-job-admin -d -p 8088:8080 ccf/ccf-job-admin:v1.0
#fi
# 2.启动ccf-flow
#if [ `grep -c "ccf-business/ccf-flow" $FIND_FILE` -ne '0' ] \
#    || [ `grep -c "ccf-common" $FIND_FILE` -ne '0' ] \
#    || [ `grep -c "ccf-service" $FIND_FILE` -ne '0' ] \
#    || [ `grep -c "ccf-framework" $FIND_FILE` -ne '0' ] \
#    || [ `grep -c "ccf-model" $FIND_FILE` -ne '0' ] \
#    || [ `grep -c "ccf-config" $FIND_FILE` -ne '0' ] \
#    || [ `grep -c "ccf-log" $FIND_FILE` -ne '0' ] \
#    || [ `grep -c "ccf-component" $FIND_FILE` -ne '0' ] \
#    || [[ "${ALL_BUILD}" == "true" ]] ;then
#  #删除容器
#  if [ $(docker ps -aq --filter name=ccf-flow) ]; then
#    docker rm -f ccf-flow
#  fi
#  #删除镜像
#  if [[ $(docker images |grep ccf/ccf-flow-platform) ]]; then
#    docker rmi ccf/ccf-flow-platform:v1.0
#  fi
#  cd ${curPath}/ccf-business/ccf-flow
#  mvn clean install -Dmaven.test.skip=true -P dev
#  docker build   -t ccf/ccf-flow-platform:v1.0 .
#  docker run --restart=always   --name ccf-flow -d -p 8223:8223 ccf/ccf-flow-platform:v1.0
#fi

# 3.启动ccf-etl
#if [ `grep -c "ccf-business/ccf-etl" $FIND_FILE` -ne '0' ] \
#    || [ `grep -c "ccf-business/ccf-database" $FIND_FILE` -ne '0' ] \
#    || [ `grep -c "ccf-common" $FIND_FILE` -ne '0' ] \
#    || [ `grep -c "ccf-service" $FIND_FILE` -ne '0' ] \
#    || [ `grep -c "ccf-framework" $FIND_FILE` -ne '0' ] \
#    || [ `grep -c "ccf-model" $FIND_FILE` -ne '0' ] \
#    || [ `grep -c "ccf-config" $FIND_FILE` -ne '0' ] \
#    || [ `grep -c "ccf-log" $FIND_FILE` -ne '0' ] \
#    || [ `grep -c "ccf-component" $FIND_FILE` -ne '0' ] \
#    || [[ "${ALL_BUILD}" == "true" ]] ;then
#  #删除容器
#  if [ $(docker ps -aq --filter name=ccf-etl) ]; then
#    docker rm -f ccf-etl
#  fi
#  #删除镜像
#  if [[ $(docker images |grep ccf/ccf-etl-platform) ]]; then
#    docker rmi ccf/ccf-etl-platform:v1.0
#  fi
#  cd ${curPath}/ccf-business/ccf-etl
#  mvn clean install -Dmaven.test.skip=true -P dev
#  docker build  -t ccf/ccf-etl-platform:v1.0 .
#  docker run --restart=always   --name ccf-etl -d -p 8221:8221 ccf/ccf-etl-platform:v1.0
#fi
# 4.启动ccf-source
if [ `grep -c "ccf-business/ccf-source" $FIND_FILE` -ne '0' ] \
    || [ `grep -c "common-utils" $FIND_FILE` -ne '0' ] \
    || [ `grep -c "public-service" $FIND_FILE` -ne '0' ] \
    || [ `grep -c "public-framework" $FIND_FILE` -ne '0' ] \
    || [ `grep -c "common-model" $FIND_FILE` -ne '0' ] \
    || [ `grep -c "common-config" $FIND_FILE` -ne '0' ] \
    || [ `grep -c "common-link" $FIND_FILE` -ne '0' ] \
    || [ `grep -c "common-log" $FIND_FILE` -ne '0' ] \
    || [[ "${ALL_BUILD}" == "true" ]] ;then
  #删除容器
  if [ $(docker ps -aq --filter name=ccf-source) ]; then
    docker rm -f ccf-source
  fi
  #删除镜像
  if [[ $(docker images |grep ccf/ccf-source-platform) ]]; then
    docker rmi ccf/ccf-source-platform:v1.0
  fi
  cd ${curPath}/ccf-business/ccf-source
  mvn clean install -Dmaven.test.skip=true -P dev
  docker build  -t ccf/ccf-source-platform:v1.0 .
  docker run --restart=always   --name ccf-source -d -p 8112:8112 ccf/ccf-source-platform:v1.0
fi

# 3.启动common-gateway
if [ `grep -c "common-gateway" $FIND_FILE` -ne '0' ] \
    || [[ "${ALL_BUILD}" == "true" ]] ;then
  #删除容器
  if [ $(docker ps -aq --filter name=common-gateway) ]; then
    docker rm -f common-gateway
  fi
  #删除镜像
  if [[ $(docker images |grep ccf/common-gateway-platform) ]]; then
    docker rmi ccf/common-gateway-platform:v1.0
  fi
  cd ${curPath}/common-gateway
  mvn clean install -Dmaven.test.skip=true -P dev
  docker build  -t ccf/common-gateway-platform:v1.0 .
  docker run --restart=always   --name common-gateway -d -p 8110:8110 ccf/common-gateway-platform:v1.0
fi

# 6.启动ccf-doc
if [ `grep -c "ccf-doc" $FIND_FILE` -ne '0' ] ;then
  #删除容器
  if [ $(docker ps -aq --filter name=ccf-doc) ]; then
    docker rm -f ccf-doc
  fi
  #删除镜像
  if [[ $(docker images |grep ccf/ccf-doc-platform) ]]; then
    docker rmi ccf/ccf-doc-platform:v1.0
  fi
  cd ${curPath}/ccf-backend/ccf-doc
  mvn clean install -Dmaven.test.skip=true -P dev
  docker build    -t ccf/ccf-doc-platform:v1.0 .
  docker run --restart=always   --name ccf-doc -d -p 8116:8116 ccf/ccf-doc-platform:v1.0
fi
# 6.启动dubbo服务
if [ `grep -c "common-dubbo-producer" $FIND_FILE` -ne '0' ] ;then
mvn clean package -Dmaven.test.skip=true
#删除容器
if [ $(docker ps -aq --filter name=common-dubbo-producer) ]; then
  docker rm -f common-dubbo-producer
fi
#删除镜像
if [[ $(docker images |grep ccf/common-dubbo-producer) ]]; then
  docker rmi ccf/common-dubbo-producer:v1.0
fi
cd ${curPath}/common-dubbo/common-dubbo-producer
docker build  -t ccf/common-dubbo-producer:v1.0 .
docker run --restart=always   --name common-dubbo-producer -d -p 8117:8117 -p 12333:12333  ccf/common-dubbo-producer:v1.0
fi